package com.lightdz.tshirtdz.bll;

import com.lightdz.tshirtdz.dal.impl.BrowserDal;
import com.lightdz.tshirtdz.dal.impl.ProductDal;
import com.lightdz.tshirtdz.model.Enum.ProductTypeEnum;
import com.lightdz.tshirtdz.model.api.ApiProduct;
import com.lightdz.tshirtdz.model.api.ApiResponse;
import com.lightdz.tshirtdz.model.entity.BrowseHistoryEntity;
import com.lightdz.tshirtdz.model.entity.ProductEntity;

import com.lightdz.tshirtdz.utils.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */
@Repository
public class BrowserHistoryeBll {

    @Autowired
    BrowserDal browserDal;
    @Autowired
     ProductDal productDal;


    /**
     * 删除浏览记录
     *
     * @param wxId
     * @return
     */
    public ApiResponse<Boolean> deleteBrowser(String wxId, long pid) {
        ApiResponse<Boolean> response = null;
        try {
            boolean result = browserDal.deleteBrowserHistory(wxId, pid);
            response = new ApiResponse<Boolean>(result);
        } catch (Exception e) {
            LogHelper.error("BrowserHistoryeBll.deleteBrowser", e);
        }
        return response;
    }

    /**
     * 获取我的浏览记录
     *
     * @param wxId
     * @return
     */
    public ApiResponse<List<ApiProduct>> getBrowseList(String wxId) {

        ApiResponse<List<ApiProduct>> apiResponse = ApiResponse.getNoResponse();
        List<ApiProduct> result = new ArrayList<>();

        try {
            //拿到所有的浏览记录列表
            List<BrowseHistoryEntity> broList = browserDal.getBrowserList(wxId);
            if (!broList.isEmpty()) {

                ApiProduct product = new ApiProduct();

                //region 组装返回结果
                for (BrowseHistoryEntity p : broList) {
                    ProductEntity pEntity = productDal.getProduct(p.getPid());
                    if (pEntity != null) {
                        product.setDesc(pEntity.getpDesc());
                        product.setBannerPicUrl(pEntity.getpBannerPicUrl());
                        product.setPrice((pEntity.getpPrice()));
                        //把字符串转还成枚举
                        product.setProductType(ProductTypeEnum.valueOf(pEntity.getpType()));
                        product.setName(pEntity.getpName());
                        ProductTypeEnum productTypeEnum = null;
                        switch (pEntity.getpType()) {
                            case "ActivityCloth":
                                productTypeEnum = ProductTypeEnum.ActivityCloth;
                                break;
                            case "Couple":
                                productTypeEnum = ProductTypeEnum.Couple;
                                break;
                            case "Polo":
                                productTypeEnum = ProductTypeEnum.Polo;
                                break;
                            case "SchoolCloth":
                                productTypeEnum = ProductTypeEnum.SchoolCloth;
                                break;
                            case "TShirt":
                                productTypeEnum = ProductTypeEnum.TShirt;
                                break;
                        }
                        product.setProductType(productTypeEnum);
                        product.setPid(p.getPid());

                        result.add(product);
                        apiResponse = new ApiResponse<>(result);
                    }
                }
                //endregion

            }
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("BrowserHistoryeBll.getBrowseList", e);
        }
        return apiResponse;
    }

    /**
     * 判断某一条浏览记录是否存在
     *
     * @param wxId
     * @param pid
     * @return
     */
    public boolean ExistBrowseHistroy(String wxId, long pid) {

        int exist = browserDal.ExistBrowseHistroy(wxId, pid);
        return exist > 0;
    }

    public boolean addBrowser(String wxId, long pid) {
        return browserDal.addBrowserHistory(wxId, pid);
    }

    public boolean updateBrowserTime(String wxId, long pid) {
        return browserDal.updateBrowserTime(wxId, pid, new Timestamp(new Date().getTime()));
    }
}
